package com.atguigu.mvc.controller;

import com.atguigu.mvc.bean.User;
import com.atguigu.mvc.bean.User2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
public class HttpController {

    private Logger logger = LoggerFactory.getLogger(HttpController.class);

    @RequestMapping("/testRequestBody")
    @ResponseBody
    public String testRequestBody(@RequestBody String requestBody) {
        System.out.println("requestBody:" + requestBody);
        return requestBody;
    }

    @RequestMapping("/testRequestBody2")
    @ResponseBody
    public String testRequestBody2(String requestBody2) {
        System.out.println("requestBody2:" + requestBody2);
        return requestBody2;
    }

    @RequestMapping("/testRequestBody3")
    @ResponseBody
    public User testRequestBody3(User requestBody3) {
        System.out.println("requestBody3:" + requestBody3);
        return requestBody3;
    }


    /**
     * 请求增加http头字段Content-Type:application/json(说明body体里面的参数是json格式)
     *
     * @param requestBody4
     * @return \@RequestBody可以获取请求体，需要在控制器方法设置一个形参，使用@RequestBody进行标识，当前请求的请求体就会为当前注解所标识的形参赋值
     */
    @RequestMapping("/testRequestBody4")
    @ResponseBody
    public User testRequestBody4(@RequestBody User requestBody4) {
        System.out.println("requestBody4:" + requestBody4);
        return requestBody4;
    }

    @RequestMapping("/testRequestEntity")
    @ResponseBody
    public String testRequestEntity(RequestEntity<String> requestEntity) {
        HttpHeaders headers = requestEntity.getHeaders();
        for (Map.Entry<String, List<String>> item : headers.entrySet()) {
            String key = item.getKey();
            List<String> values = item.getValue();
            logger.info("key={}, values={}, length={}", key, Arrays.toString(values.toArray()), values.size());
        }

        //当前requestEnity表示整个请求报文的信息
        System.out.println("请求头：" + requestEntity.getHeaders());
        System.out.println("请求体：" + requestEntity.getBody());
        return requestEntity.getBody();
    }

    @RequestMapping("/testResponseBody")
    @ResponseBody
    public String testResponseBody() {
        return "success";
    }


    @RequestMapping("/testResponseUser")
    @ResponseBody
    public User2 testResponseUser(HttpServletRequest request) {
        String header = request.getHeader("x-requested-with");
        System.out.println("x-requested-with = " + header);

        boolean xmlHttpRequest = request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equals("XMLHttpRequest");
        System.out.println("xmlHttpRequest = " + xmlHttpRequest);
        return new User2(1001, "admin", "123456", 23, "男");
    }


    @RequestMapping({"/testAjax", "/testAxios"})
    @ResponseBody
    public String testAjax(String username, String password,
                           HttpServletRequest request, RequestEntity<String> requestEntity) {
        HttpHeaders headers = requestEntity.getHeaders();
        for (Map.Entry<String, List<String>> item : headers.entrySet()) {
            String key = item.getKey();
            List<String> values = item.getValue();
            logger.info("key={}, values={}, length={}", key, Arrays.toString(values.toArray()), values.size());
        }

        //当前requestEnity表示整个请求报文的信息
        System.out.println("请求头：" + requestEntity.getHeaders());
        System.out.println("请求体：" + requestEntity.getBody());

        System.out.println("username:" + username + ",password:" + password);
        String header = request.getHeader("x-requested-with");
        System.out.println("x-requested-with = " + header);

        boolean xmlHttpRequest = request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equals("XMLHttpRequest");
        System.out.println("xmlHttpRequest = " + xmlHttpRequest);
        return "hello,ajax " + request.getRequestURI();
    }


}
